package remoteControl.panel;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ObtenerDatosGPS extends Activity 
{    
 BluetoothAdapter btAdapter;
 BluetoothSocket btSocket;
 public BluetoothDevice device;
 Button getDataGPS;
 Button getPosGPS;
 TextView tv;
 String message;
 
 @Override
 public void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.gps);
  
  getDataGPS = (Button) findViewById(R.id.button1);
  getDataGPS.setOnClickListener(sendMsgListener);
  
  getPosGPS = (Button) findViewById(R.id.button2);
  getPosGPS.setOnClickListener(getPosGPSListener);
  
  tv = (TextView) findViewById(R.id.textView1);
  
  
  final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  final String btAdress = "00:06:66:07:AC:2A";
  btAdapter = BluetoothAdapter.getDefaultAdapter();
  device = btAdapter.getRemoteDevice(btAdress);
  
  try 
  {
   btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
   btSocket.connect();
  } 
  catch (IOException e) 
  {
   Toast.makeText(getApplicationContext(), "Could not connect socket", Toast.LENGTH_LONG).show();
  }
  
 }
 
private String listenForMessage(int op)
{
 String result = "";
 int bufferSize = 1024;
 byte[] buffer = new byte[bufferSize];
 try 
 {
  InputStream instream = btSocket.getInputStream();
  int bytesRead =- 1;
  while (true) 
  {
   bytesRead = instream.read(buffer);
   if (bytesRead != -1) 
   {
    while ((bytesRead == bufferSize) && (buffer[bufferSize-1] != 0))
    {
     message = message + new String(buffer, 0, bytesRead);
     bytesRead = instream.read(buffer);
    }
    message = new String(buffer, 0, bytesRead - 1);
    message = message.substring(0,20);
    if( (message.startsWith("[")) && (message.contains("]")) )
    {
    	message = message.substring(1,19);
    	Toast.makeText(getApplicationContext(),message, Toast.LENGTH_LONG).show(); 
    	if( op == 1)
    	{
    	 tv.setText(message);
    	    	
    	}
    	else
    	{
    	 Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://maps.google.com/?q=" + message));
         startActivity(viewIntent);  
    	}
    }
   

    return result;
   }
  }
 }
 catch (IOException e) {Toast.makeText(getApplicationContext(), "Could not listen socket", Toast.LENGTH_LONG).show();}
return result;
}

@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		try {
			btSocket.close();
			finish();
		} catch (IOException e) {
		}
	}
 
@Override
protected void onStop() {
	// TODO Auto-generated method stub
	super.onDestroy();
	try {
		btSocket.close();
		finish();
	} catch (IOException e) {
	}
}
  

 
 private Button.OnClickListener sendMsgListener = new Button.OnClickListener() 
 {
  public void onClick(View v) 
  {
	  listenForMessage(1);
  }
  };
  
  private Button.OnClickListener getPosGPSListener = new Button.OnClickListener() 
  {
   public void onClick(View v) 
   {
 	  listenForMessage(2);
   }
   };
  
  
  
}     
